package www.dashu.com.controller;

import www.dashu.com.api.CommonResult;
import www.dashu.com.service.RedisService;
import www.dashu.com.domain.SysRole;
import www.dashu.com.domain.SysRolePerms;
import www.dashu.com.dto.RoleQuery;
import www.dashu.com.service.SysPermissionService;
import www.dashu.com.service.SysRolePermsService;
import www.dashu.com.service.SysRoleService;
import www.dashu.com.service.SysUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 后台用户管理
 */
@Controller
@Api(tags = "SysRoleController", description = "角色管理")
@RequestMapping("/role")
public class SysRoleController
{

    @Autowired
    SysRoleService roleService;
    @Autowired
    SysRolePermsService rolePermsService;
    @Autowired
    RedisService redisService;
    @Autowired
    SysPermissionService permissionService;
    @Autowired
    SysUserRoleService userRoleService;

    @ApiOperation(value = "角色列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Page<SysRole>> list(RoleQuery roleQuery) {

        try {
            Page<SysRole> pageData = roleService.findAll(roleQuery);
            return CommonResult.success(pageData);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @ApiOperation(value = "新增角色")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody SysRole role) {
        role.setId(roleService.createId(role));
        role.setCreateTime(new Date());
        role.setUpdateTime(new Date());
        roleService.save(role);
        permissionService.initResourceRolesMap();
        return CommonResult.success();
    }

    @ApiOperation(value = "修改角色")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody SysRole role) {
        role.setUpdateTime(new Date());
        roleService.save(role);
        permissionService.initResourceRolesMap();
        return CommonResult.success();
    }

    @ApiOperation(value = "删除角色")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor = Exception.class)
    public CommonResult delete(@RequestBody SysRole role) {
        roleService.deleteById(role.getId());
        rolePermsService.deleteSysRolePermsByRoleId(role.getId());
        userRoleService.deleteSysUserRoleByRoleId(role.getId());
        permissionService.initResourceRolesMap();
        return CommonResult.success();
    }

    @ApiOperation(value = "更新角色权限")
    @RequestMapping(value = "/permissionUpdate", method = RequestMethod.POST)
    @ResponseBody
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor = Exception.class)
    public CommonResult roleUpdate(@RequestBody SysRole role) {
        rolePermsService.deleteSysRolePermsByRoleId(role.getId());
        List<SysRolePerms> list = new ArrayList<>();
        for(Long pid : role.getPerms()){
            SysRolePerms rolePerms = new SysRolePerms();
            rolePerms.setId(rolePermsService.createId(rolePerms));
            rolePerms.setRId(role.getId());
            rolePerms.setPId(pid);
            rolePerms.setCreateTime(new Date());
            rolePerms.setUpdateTime(new Date());
            list.add(rolePerms);
        }
        rolePermsService.saveAll(list);
        permissionService.initResourceRolesMap();
        return CommonResult.success();
    }

}
